package com.tgy.reflection;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/***
 * @ClassName: TreflectionT
 * @Description: 通过反射获取泛型
 * @Auther: 送一一块二向箔
 * @Date: 20:42 2021/12/1
 * @version : V1.0
 */
public class TreflectionT {
    public static void main(String[] args) throws NoSuchMethodException {
        Method method1 = TreflectionT.class.getMethod("test01", Map.class, List.class);

        //获得泛型参数类型
        Type[] genericParameterTypes = method1.getGenericParameterTypes();
        for (Type genericParameterType : genericParameterTypes) {
            System.out.println(genericParameterType);
            //如果得到是结构化参数类型，就强转出来
            if(genericParameterType instanceof ParameterizedType){
                //强转成ParameterizedType并获得真实类型
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }
            Method method2 = TreflectionT.class.getMethod("test02", null);
            Type genericReturnType = method2.getGenericReturnType();

            System.out.println(genericReturnType);
            System.out.println("-===================");
            //如果得到是结构化参数类型，就强转出来
            if(genericParameterType instanceof ParameterizedType){
                //强转成ParameterizedType并获得真实类型
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }

        }


    }
    public void test01(Map<String,User> map, List<User> list){
        System.out.println("test01");
    }
    public Map<String,User> test02(){
        System.out.println("test02");
        return null;
    }
}
